//
//  SHChangeCardController.m
//  JiFengDaRen
//
//  Created by SH_iOS on 2017/10/17.
//  Copyright © 2017年 SH_iOS. All rights reserved.
//
#import "SHBankCardListView.h"
#import "XDActionSheet.h"//底部弹出菜单
#import "SHWithdrawCashBankCardController.h"
#import "SHChangeCardController.h"
#import "SHBankCarCell.h"
#import "SHChangePaymentCell.h"
#import "SHChangeCardHeaderView.h"
#import "SHChangeCardCell.h"
#import "SHChangeFooter.h"

@interface SHChangeCardController ()<UITableViewDelegate,UITableViewDataSource,SHChangeFooterDelegate,SHWithdrawCashBankCardControllerDelegate,XDActionSheetDelegate,XDActionSheetDatasource,SHBankCardListViewDelegate>

@property(nonatomic,strong)UITableView *tableView;
@property(nonatomic,strong)NSString *paymentName;//支付方式名称
@property(nonatomic,assign)BOOL  isClickFooter;//支付方式名称
@property(nonatomic,assign)int paymentType;//支付方式类型
@property(nonatomic,strong)BankCardModel *oldCashCardModel;//收款卡信息
@property(nonatomic,strong)BankCardModel *cashCardModel;//收款卡信息

@property(nonatomic,strong)SHChangeFooter *footer;
@property(nonatomic,strong)XDActionSheet *paymentsSheet;//底部弹出菜单列表
@property(nonatomic,strong)NSArray *paymentTitleArray;//底部弹出菜单标题数组


@end

@implementation SHChangeCardController


- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"修改收款卡";
    self.paymentTitleArray = @[@"我是瀚付",@"支付宝扫码支付",@"取消"];
    self.paymentName  = @"我是瀚付";

    [self tableView];
    _isClickFooter = YES;
    [self.tableView reloadData];
    [self getPayChoiceCashType];

    // Do any additional setup after loading the view.
}

#pragma mark - 查询可用银联支付类型
- (void)getPayChoiceCashType{
    
    
    [SHHomeModelView getPayChoiceCashTypeSuccess:^(NSInteger cashType) {
        switch (cashType) {
            case 0://瀚银银联收款
                self.paymentType = 6;
                [self queryDefaultBankCardInfo];

                break;
            case 1://杉奇银联收款
                self.paymentType = 5;
                [self queryDefaultBankCardInfo];

                break;
            case 2://上福银联收款
                self.paymentType = 8;
                [self queryDefaultBankCardInfo];
                break;

            case 3:// 富汇通银联收款
                self.paymentType = 7;
                [self queryDefaultBankCardInfo];
                break;

            default:
                break;
        }


    }];

}




- (UITableView *)tableView{
    
    if (!_tableView) {
        
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, KScreenWidth, KScreenHeight - KNAVIGATIONBARHEIGHT) style:UITableViewStylePlain];
        _tableView.backgroundColor = KBASEBGCOLOR;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        [self.view addSubview:_tableView];
        _tableView.separatorStyle =  UITableViewCellSeparatorStyleNone;
        [_tableView registerClass: [SHChangeCardCell  class]  forCellReuseIdentifier:@"BankCard"];
        [_tableView registerClass: [SHChangePaymentCell  class]  forCellReuseIdentifier:@"PaymentwayCell"];
        [_tableView registerClass:[SHChangeFooter class] forCellReuseIdentifier:@"SHChangeFooter"];
    }
    return _tableView;
}



#pragma mark --- UITablView  & DataSource  & Delegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    
    return 2;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    if (section == 1) {
        
        return   2;
    }
    return 1;
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    
    return  __I6Height(15);
}




- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.section == 0) {
        
        return __I6Height(44);
    }else{
        
        if (indexPath.row == 0) {
            
            return __I6Height(60);
            
        }else{
            
            return __I6Height(142);
        }
    }
    
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    
    SHChangeCardHeaderView *header = [[SHChangeCardHeaderView alloc]initWithFrame:CGRectMake(0,0, KScreenWidth,__I6Height(20))];
    
    

//    if (section == 0) {
//        
//        header.headerTitle = @"选择支付方式";
//
//    }else{
//
//        header.headerTitle = @"当前收款卡";
//
//    }
//
    return  header;
    
}




#pragma mark - cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.section == 0) {
        
        SHChangePaymentCell *cell = [tableView dequeueReusableCellWithIdentifier:@"PaymentwayCell"];
        cell.paymentName = self.paymentName;
    
        return  cell;
        
    }else{
        
        if (indexPath.row == 0) {
            
            SHChangeCardCell *cell = [tableView dequeueReusableCellWithIdentifier:@"BankCard"];
            
            cell.model = self.cashCardModel;
            
            return cell;
        }else{
            
            _footer = [tableView dequeueReusableCellWithIdentifier:@"SHChangeFooter"];
            _footer.buttonTitle = @"确认修改";
            _footer.delegate = self;
            _footer.selectionStyle = UITableViewCellSelectionStyleNone;
            _footer.isClickedBtn = _isClickFooter;
            return _footer;
        }
       
    }
    
}




#pragma mark - 点击列表
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if (indexPath.section == 0) {//选择支付方式
      
         [self.paymentsSheet actionSheetShow:self.paymentTitleArray.count];
        
    }else if(indexPath.row == 0){// 点击了收款卡
        
        if (!self.oldCashCardModel.bank_card_code) {
            
            return;
        }
        [MBProgressHUD showHUDAddedTo:self.view animated:YES];
        [SHUserCenterHttp getBankCardLsitWithUsingChoices:1  bankCardType:11  success:^(NSMutableArray *bankCardListArray) {
            [MBProgressHUD  hideHUDForView:self.view animated:YES];
            if (!bankCardListArray) {
                [self.view makeToast:@"没有收款银行卡" duration:2.f position:@"center"];
                return ;
            }
           NSMutableArray *bankCardListMutaArray = [NSMutableArray arrayWithCapacity:10];
            for (BankCardModel *model in bankCardListArray) {
                if (model.auth_4_status ) {
                    model.bank_card_type = 11;
                    [bankCardListMutaArray addObject:model];
                    
                }
            }
            
            SHBankCardListView *payBankListView = [[SHBankCardListView alloc]initWithFrame:CGRectMake(0, 0, KScreenWidth, KScreenHeight) bankCardListArray:bankCardListMutaArray];
            payBankListView.delegate = self;
            [payBankListView showBankCardListView];
            
        } complection:^(id result) {
            [MBProgressHUD  hideHUDForView:self.view animated:YES];
            
        } failure:^(id erro) {
            [MBProgressHUD  hideHUDForView:self.view animated:YES];
            
        }];
        
        
        
        
        
    }
    
    
    
}



#pragma mark - 底部菜单列表代理
static NSString *xdActionSheetPaymentReuserId = @"xdActionSheetPaymentReuserId";
- (XDActionSheet *)paymentsSheet{
    
    
        
        _paymentsSheet = [[XDActionSheet alloc]initWithFrame:CGRectZero];
        _paymentsSheet.delegate = self;
        _paymentsSheet.datasource = self;
        [_paymentsSheet registerClass:[UITableViewCell class] forCellReuseIdentifier:xdActionSheetPaymentReuserId];
    
    
    [[UIApplication sharedApplication].keyWindow addSubview:_paymentsSheet];
    return _paymentsSheet;
}

- (NSInteger)popoverListView:(XDActionSheet *)sheet numberOfRowsInSection:(NSInteger)section{
    
    
    return  self.paymentTitleArray.count - 1;
    
}

- (UITableViewCell *)popoverListView:(XDActionSheet *)sheet cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        UITableViewCell *cell = [sheet dequeueReusablePopoverCellWithIdentifier:xdActionSheetPaymentReuserId];
        
        cell.textLabel.text = self.paymentTitleArray[indexPath.row];
        cell.textLabel.textAlignment = NSTextAlignmentCenter;
        cell.textLabel.font = KSHSystemFont(15);
        if (indexPath.row < self.paymentTitleArray.count - 2) {
            UIView *line = [[UIView alloc]initWithFrame:CGRectMake(0, __I6Height(45)-0.5, [UIScreen mainScreen].bounds.size.width,0.5)];
            line.backgroundColor = KLINECOLOR;
            
            
            [cell addSubview:line];
        }
        
        return cell;
        
    }else{
        UITableViewCell *cell = [sheet dequeueReusablePopoverCellWithIdentifier:xdActionSheetPaymentReuserId];
        cell.textLabel.textAlignment = NSTextAlignmentCenter;
        cell.textLabel.font = KSHSystemFont(15);
        
        cell.textLabel.text = [self.paymentTitleArray lastObject];
        return cell;
        
    }
    
    
}

//====== 切换支付方式======
- (void)popoverListView:(XDActionSheet *)sheet didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.section == 0) {
        
        self.paymentName = self.paymentTitleArray[indexPath.row];
        
        if ([self.paymentName isEqualToString:@"杉德收款"]) {
            
            self.paymentType = 4 ;
            
        }else if([self.paymentName isEqualToString:@"微信扫码支付"]){
            
              self.paymentType = 10;
            
        }else if([self.paymentName isEqualToString:@"支付宝扫码支付"]){
            
              self.paymentType = 20;
            
        
            
        }else if([self.paymentName isEqualToString:@"我是瀚付"]){
            
            
        }
        [self queryDefaultBankCardInfo];
        [self.tableView reloadData];
    }
   
    
}






#pragma mark - 银行卡弹出列表代理
- (void)bankCardListView:(SHBankCardListView *)view bankCardModel:(BankCardModel *)bankCardModel{
    
    if (self.oldCashCardModel._id == bankCardModel._id) {
        
        _isClickFooter = NO;
        [self.tableView reloadData];

        return;
    }
    self.cashCardModel = bankCardModel;
    _isClickFooter = YES;
    _footer.isClickedBtn = YES;

    [self.tableView reloadData];
    
}




#pragma mark - 点击确认
- (void)changeFooter:(SHChangeFooter *)footer changeCardBtn:(UIButton *)sender{
    
   
    
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    [MyDataService requestAFURL:@"/payment_base/api/modify_default_debit_card" httpMethod:@"POST" params:@{@"payment_type":@(self.paymentType),@"bank_card_id":@(self.cashCardModel._id)} data:nil complection:^(id result) {
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        SHLog(@"succes == %@",result);
        
        BOOL successed = [result[@"success"] boolValue];
        if (successed) {
            [self.view makeToast:@"修改成功" duration:2.f position:@"center"];
            self.oldCashCardModel = self.cashCardModel;
            _isClickFooter = NO;
            _footer.isClickedBtn = NO;
            [self.tableView reloadData];
            
        }else{
            [self.view makeToast:result[@"data"] duration:2.f position:@"center"];
        }
    } failure:^(id erro) {
        [MBProgressHUD hideHUDForView:self.view animated:YES];

        SHLog(@"erro == %@",erro);
        
    }];
     
    
}



#pragma mark - 根据支付方式查找默认收款卡
- (void)queryDefaultBankCardInfo{
    
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    [MyDataService requestAFURL:@"/payment_base/api/query_default_debit_card" httpMethod:@"GET" params:@{@"payment_type":@(self.paymentType)} data:nil complection:^(id result) {
        [MBProgressHUD hideHUDForView:self.view animated:YES];

        BOOL successed = [result[@"success"] boolValue];
        if (successed) {
            self.cashCardModel = [BankCardModel mj_objectWithKeyValues:result[@"data"]];
            self.oldCashCardModel = self.cashCardModel;
            _isClickFooter = NO;
            _footer.isClickedBtn = NO;
            [self.tableView reloadData];
            
        }else{
            _isClickFooter = NO;
            _footer.isClickedBtn = NO;
            [self.view makeToast:result[@"data"] duration:2.f position:@"center"];
        }
        
        SHLog(@"succes == %@",result);
    } failure:^(id erro) {
        
        [MBProgressHUD hideHUDForView:self.view animated:YES];

        SHLog(@"erro == %@",erro);
        
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
